package com.neusoft.neu24.nep.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;


@Slf4j
public class CorsGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
       /*
        return chain.filter(exchange)
                .then(Mono.fromRunnable(
                        () -> log.info("Global Post Filter Execute...")
                ));
        */

        System.out.println(" 1111111111111111111111111   ");
        System.out.println(" 222222222222222222222222  添加跨越请求信息 !!!!!!  - - - - -  -   -   ");
        log.info("请求参数中不包含custom参数, 请求非法, 拦截");
        exchange.getResponse().setStatusCode(HttpStatus.OK);
        exchange.getResponse().getHeaders().add("Content-Type", "application/json;charset=UTF-8");
        exchange.getResponse().getHeaders().add("Access-Control-Allow-Origin", "*");
        exchange.getResponse().getHeaders().add("Access-Control-Allow-Headers", "*");
        exchange.getResponse().getHeaders().add("Access-Control-Allow-Methods", "*");
        exchange.getResponse().getHeaders().add("Access-Control-Allow-Credentials", "true");

        Mono<Void> filter = chain.filter(exchange);
               return filter;




    }

    @Override
    public int getOrder() {
        return   Ordered.HIGHEST_PRECEDENCE;
    }
}